<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {
	//登录
    public function index(){
		$data['username']=I('post.account');
		$data['password']=md5(I('post.password'));
		$data['auto']=I('post.auto');
		$data['logintime']=time();
		$db=M('user');
		$user=$db->field('username,password,uid,lock')->where(array('username'=>$data['username']))->find();
		if($user['username']!=$data['username']) $this->error('用户名不正确！');
		if($user['password']!=$data['password']) $this->error('密码不正确！');
		if($user['lock']==1) $this->error('用户已经被锁定，请联系管理员');
		//每天登录经验增加20
		$this->leve_exp($user['uid']);
		
		//登录成功，保存数据
		$db->where(array('uid'=>$user['uid']))->save($data);
		
		//自动登录，写入cookie，读取配置项设置的保存时间
		if($data['auto']=='on'){
			cookie(session_name(),session_id(),C('COOKIE_TIME'));
		}
		session('username',$data['username']);
		session('uid',$user['uid']);
		
		$this->success('登录成功！'.$abc);
    }
	//登录增加经验
	private function leve_exp($uid){
		$logintime=M('user')->where(array('uid'=>$uid))->getField('logintime');
		//获得当天时间戳
		$day=strtotime(date('Y-m-d'));
		if($logintime<$day){
			M('user')->where(array('uid'=>$uid))->setInc('exp',C('LOGIN_EXP')); 
		}
	}
	
	//退出
	public function out(){
		session('username',null);
		session('uid',null);
		//unset($_SESSION['username']);
		//unset($_SESSION['uid']);
		$this->error('退出成功',__APP__);
	}
	//ajax
	public function check_user(){
		if(!IS_AJAX){ $this->error('页面不存在！'); }
		$username=I('post.param');
		$user=M('user')->where(array('username'=>$username))->getField('username');
		if($user){
			$status=array(
				'info'=>'用户名存在',
				'status'=>'y'
			);
			$this->ajaxReturn($status);
		}else{
			$status=array(
				'info'=>'用户名不存在',
				'status'=>'n'
			);
			$this->ajaxReturn($status);
		}
	}
	
}